package com.wc.alorithm_blue_bridge._数据结构.并查集.缴纳过路费_扩展;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/3/27 10:38
 * @description
 * https://www.lanqiao.cn/courses/46292/learning/?id=3440227&compatibility=false
 */
public class Main {
    /**
     * 思路：
     * 将边权 <= R 的节点放在一个集合中, 集合中任意选择两个点所在的路径必定 <= R, 求出价值 <= R的所有点对
     * 同理, 求出 <= L - 1 的, 二者相减就实现了 [L, R] 之间的点对个数
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int[] p = new int[N], u = new int[N], v = new int[N], w = new int[N];
    static int[] siz = new int[N];
    static int n, m, L, R;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        L = sc.nextInt();
        R = sc.nextInt();
        for (int i = 1; i <= m; i++) {
            u[i] = sc.nextInt();
            v[i] = sc.nextInt();
            w[i] = sc.nextInt();
        }
        long res = 0;
        res = work(R, 1, res);
        res = work(L - 1, -1, res);
        out.println(res);
        out.flush();
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    static long work(int bound, int sign, long res) {
        // 存入 <= L 的路长连接在一起
        for (int i = 1; i <= n; i++) {
            p[i] = i;
            siz[i] = 0;
        }
        for (int i = 1; i <= m; i++) {
            if (w[i] > bound) continue;
            int a = find(u[i]), b = find(v[i]);
            if (a == b) continue;
            p[a] = b;
        }
        for (int i = 1; i <= n; i++) {
            int x = find(i);
            siz[x]++;
        }
        for (int i = 1; i <= n; i++) res += sign * (long) (siz[i] - 1) * (siz[i] - 2) / 2;

        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

